import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {adSystemApiUrl, tpType} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';
import {LocalStorageService} from '../../lib/service/local-storage.service';

@Injectable()
export class AccountService {

  constructor(private httpService: HttpService, private localStorageService: LocalStorageService) {
  }

  // 获取账户详情
  getAccountDetail() {
    const url = `${adSystemApiUrl}/v2/member/tuike/account/details`;
    const body = JSON.stringify({
      applet_uuid: this.localStorageService.getNowApplet().applet_uuid,
    });
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 获取省列表
  getProvinceList() {
    const url = `${adSystemApiUrl}/v1/member/comm/address/province_list`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  // 获取市列表
  getCityList(province_id) {
    const url = `${adSystemApiUrl}/v1/member/comm/address/city_list?province_id=${province_id}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  // 获取区列表
  getRegionList(city_id) {
    const url = `${adSystemApiUrl}/v1/member/comm/address/region_list?city_id=${city_id}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  // 获取经营类目列表
  getBusinessTypeList() {
    const url = `${adSystemApiUrl}/v2/member/tuike/business_type/list`;
    const body = JSON.stringify({
      applet_uuid: this.localStorageService.getNowApplet().applet_uuid,
      pageSize: 999
    });
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 商户扩展信息修改
  updateAccountDetail(body) {
    const url = `${adSystemApiUrl}/v2/member/tuike/account/update_details`;
    body.applet_uuid =  this.localStorageService.getNowApplet().applet_uuid;
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 获取充值列表
  getTopupList(page, pageSize, pay_type, pay_time_start, pay_time_end, sort, data_status) {
    const url = `${adSystemApiUrl}/v2/member/tuike/order/index`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const order_by = {};
    const status = [];
    if (!!sort.price) {
      const price = sort.price === 'descend' ? 'desc' : 'asc';
      order_by['price'] = price; }
    if (!!sort.pay_time) {
      const pay_time = sort.pay_time === 'descend' ? 'desc' : 'asc';
      order_by['pay_time'] = pay_time; }
    data_status.every((val, idx, array) => {
      if (val.checked) {
        if (val.value === 1) {
          status.push(2, 3);
        } else {
          status.push(1, 4);
        }
      }
      return true;
    });
    const body = JSON.stringify({
      page: page,
      pageSize: pageSize,
      pay_type: pay_type || '',
      pay_time_start: pay_time_start || '',
      pay_time_end: pay_time_end || '',
      order_by: JSON.stringify(order_by),
      status: JSON.stringify(status)
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 导出选中
  down_xsl_checked(data) {
    const url = `${adSystemApiUrl}/v2/member/tuike/order/excel`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers, responseType: 3});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 微信支付
  wxpay(body) {
    const url = `${adSystemApiUrl}/v2/member/tuike/pay/wxpay`;
    body.applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 支付宝支付
  alipay(body) {
    const url = `${adSystemApiUrl}/v2/member/tuike/pay/alipay`;
    body.applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 获取用户信息
  getUserInfo() {
    const url = `${adSystemApiUrl}/v1/member/account_extend/getAccountExtend`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  // 获取用户信息
  editUserInfo(body) {
    const url = `${adSystemApiUrl}/v1/member/account_extend/editAccountExtend`;
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  // 获取所有类目
  getAllClass() {
    const url = `${adSystemApiUrl}/v1/member/account/getAllClass?parent_id=0`;
    return this.httpService.httpPostAuth(url, {}).map(res => res);
  }

  // 获取所有类目
  getWxPayStatus(body) {
    const url = `${adSystemApiUrl}/v2/member/tuike/order/status`;
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }
}
